﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using LimitatorInterfaces;
using LimitatorInterfaces.UnloadServices;
using MicexFileUnloader;

namespace UnloadServices
{
    public class MicexFileFormatter : IUnloadFormatter
    {
        private IFormatProvider _formatProvider = CultureInfo.InvariantCulture;

        public object Format(LimitEntity source)
        {
            if (!CheckLimit(source))
                throw new UnloadFormatterException("Ошибка в лимите " + source.ToString());

            if (source.IsMoney)
                return ToMoneyLimit(source);

            return ToDepoLimit(source);
        }

        private bool CheckLimit(LimitEntity source)
        {
            if (string.IsNullOrWhiteSpace(source.ClientCode))
                return false;

            if (!source.IsMoney && string.IsNullOrWhiteSpace(source.ActiveCode))
                return false;
            
            return true;
        }

        private string ToDepoLimit(LimitEntity source)
        {
            return string.Format(_formatProvider, Res.LimitTmplDepo, source.ClientCode, source.Value, source.WaPrice, source.ActiveCode);
        }

        private string ToMoneyLimit(LimitEntity source)
        {
            return string.Format(_formatProvider, Res.LimitTmplMoney, source.ClientCode, source.Value, source.Leverage);
        }
    }
}
